# -*- coding: utf-8 -*-
from datetime import datetime
from decimal import Decimal

import simplejson


class MBJsonEncoder(simplejson.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decimal):
            return float(obj)
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        return simplejson.JSONEncoder.default(self, obj)


encoder = MBJsonEncoder(sort_keys=True, ensure_ascii=True)


def json_dumps(o):
    _ = encoder.encode(o)
    return _.encode('ascii')
